window.addEvent('domready', function(){
	var form = $('frmSearch');
	
	//pagination
	new Pagination($('nilai_pagination'),{
		onPageChange: function(obj){
			form.offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	//table header untuk sorting
	new SortableTableHeader($('nilai_table').getElement('tr[class:header]'), {
		tableFieldName:['nilai_id ','achiev_jenis','achiev_bidang','detachiev_tingkat','detachiev_nama',
		                'reward_nama','nilai_min','nilai_max','nilai_point'],
		onHeaderClick:function(item, fieldName, sort){
			form.orderby.value = fieldName+" "+sort;
			search_cont(form);
		}
	});
	
	search(form);	
});

function search(form){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			$('nilai_table_content').set('html', json.table_content);
			
			//rubah jumlah halamannya
			$('nilai_pagination').retrieve('pagination').setPageCount(Math.ceil(json.table_num_record/10));
		}
	}).send();
}

function search_cont(form){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			$('nilai_table_content').set('html', json.table_content);
		}
	}).send();
}

var TMP = {}
TMP.data = {'detachiev_id':'', 'detachiev_nama':'',
			'reward_id':'','reward_nama':'',
			'nilai_id':''};
TMP.data.deff = {'detachiev_id':'', 'detachiev_nama':'',
		'reward_id':'','reward_nama':'',
		'nilai_id':''};
function do_add(){
	APP.data = $merge(TMP.data, TMP.data.deff);
	var utils = APP.base.replace('/psdm/nilai','');
	FBModal_show2(utils +'/utils/searchDialog/detachievement/getDialog', 'post', 
		'return_fields=detachiev_id,achiev_jenis'+
		'&submit_action=do_add_callback', true, false, null, 
		{onSuccess: function(res){
			Utils.searchDialog.install({
				form:'detachievSearchFrm',
				pagination:'detachievSearchFrm_pagination',
				table: {
					element:'detachievSearchFrm_table',
					columns:['detachiev_id','detachiev_nama', 'detachiev_tingkat', 'achiev_jenis','achiev_bidang'],
					content:'detachievSearchFrm_tableContent'
				}
			});
		}
	});
}

function do_add_callback(id, jenis){
	if ($chk(id) && $chk(jenis)) TMP.data = $merge(TMP.data, {'detachiev_id':id,'achiev_jenis':jenis});
	var utils = APP.base.replace('/psdm/nilai','');
	FBModal_show2(utils +'/utils/searchDialog/rewardPunishment/getDialog/do_add_callback2', 'post', 
		'where=reward_jenis='+(jenis=="PR"? "'RE'":"'PU'")+" AND 1"+
		'&return_fields=reward_id'+
		'&submit_action=do_add_callback2', true, false, null, {
		onSuccess: function(res){
			Utils.searchDialog.install({
				form:'rewardSearchFrm',
				pagination:'rewardSearchFrm_pagination',
				table: {
					element:'rewardSearchFrm_table',
					columns:['reward_id','reward_jenis','reward_nama'],
					content:'rewardSearchFrm_tableContent'
				}
			});
		}
	});
}

function do_add_callback2(id){
	if ($chk(id)) TMP.data = $merge(TMP.data, {'reward_id':id});
	FBModal_show2(APP.base+'/ajax/addModal/'+TMP.data.detachiev_id+'/'+TMP.data.reward_id, 'post', "", true, true, null, {
		onSuccess: function(){
			new FormCheck('frmModal',{submitByAjax:true, 
		        onAjaxSuccess: function(res){
					var json = JSON.decode(res);
					FBModal_message(APP.base+'/ajax/messageDialog', "Save", json.msg);
					search($('frmSearch'));
		        }
		    });
		}
	});
}

function do_delete(rowElement, id){
	ajax(APP.base+'/ajax/delete/'+id, "post", "", $empty, function(res){
		var json = JSON.decode(res);
		if (!json.error){
			//tween opacity and destroy it
			rowElement.get('tween').chain(function(){
				rowElement.destroy();
			}).start('opacity',[1,0]);
		}
		FBModal_message(APP.base+'/ajax/messageDialog', "Delete", json.msg);
		search($('frmSearch'));
	});
}

function do_edit(id){
	TMP.data = $merge(TMP.data, TMP.data.deff);
	FBModal_show2(APP.base+'/ajax/editModal/'+id, 'post', "", true, true, null, {
		onSuccess: function(){
			new FormCheck('frmModal',{submitByAjax:true, 
		        onAjaxSuccess: function(res){
					var json = JSON.decode(res);
					FBModal_message(APP.base+'/ajax/messageDialog', "Save", json.msg);
					search($('frmSearch'));
		        }
		    });
			
			TMP.data = $merge(TMP.data, {
				detachiev_id: $$("input[name='inp[0]']")[0].value,
				reward_id: $$("input[name='inp[1]']")[0].value,
				nilai_id: $$("input[name='inp[5]']")[0].value,
			});
		}
	});
}

var rcheck =  {
	request: null,
	error: false,
	msg:''
};

function rangeCheck(el){
	var error = false;
	if (rcheck.request==null){
		rcheck.request = new Request({
			url: APP.base+'/ajax/validateRange/'+TMP.data.detachiev_id+'/'+el.value+(TMP.data.nilai_id?'/'+TMP.data.nilai_id : ""), 
			method: 'post', 
			async:false,
			onSuccess: function(responseText) {
				var json = JSON.decode(responseText)
				if (json.error){
					rcheck.msg = json.msg;
				}
				rcheck.error = json.error;
			}
		});
	}
	rcheck.request.options.url = APP.base+'/ajax/validateRange/'+TMP.data.detachiev_id+'/'+el.value+(TMP.data.nilai_id?'/'+TMP.data.nilai_id : "");
	rcheck.request.send('');
	if (rcheck.error) el.errors.push(rcheck.msg);
	return !rcheck.error;
}

var mcheck =  {
	request: null,
	error: false,
	msg:''
};

function ammountCheck(el){
	var max = el.value;
	var min = $('inp_min').value;
	var error = false;
	if (mcheck.request==null){
		mcheck.request = new Request({
			url: APP.base+'/ajax/validateAmmount/'+min+'/'+max, 
			method: 'post', 
			async:false,
			onSuccess: function(responseText) {
				var json = JSON.decode(responseText)
				if (json.error){
					mcheck.msg = json.msg;
				}
				mcheck.error = json.error;
			}
		});
	}
	mcheck.request.options.url = APP.base+'/ajax/validateAmmount/'+min+'/'+max;
	mcheck.request.send('');
	if (mcheck.error) el.errors.push(mcheck.msg);
	return !mcheck.error;
}